package br.com.knowledge.persistence.hibernate.dao;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import static org.hibernate.criterion.Example.create;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

import br.com.knowledge.persistence.hibernate.entities.CadFuncao;

/**
 * A data access object (DAO) providing persistence and search support for
 * CadFuncao entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see br.com.knowledge.persistence.hibernate.entities.CadFuncao
 * @author MyEclipse Persistence Tools
 */

public class CadFuncaoDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(CadFuncaoDAO.class);
	// property constants
	public static final String NMFUNCAO = "nmfuncao";
	public static final String DESCRICAO = "descricao";

	public void save(CadFuncao transientInstance) {
		log.debug("saving CadFuncao instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(CadFuncao persistentInstance) {
		log.debug("deleting CadFuncao instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public CadFuncao findById(java.lang.Integer id) {
		log.debug("getting CadFuncao instance with id: " + id);
		try {
			CadFuncao instance = (CadFuncao) getSession()
					.get(
							"br.com.knowledge.persistence.hibernate.entities.CadFuncao",
							id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<CadFuncao> findByExample(CadFuncao instance) {
		log.debug("finding CadFuncao instance by example");
		try {
			List<CadFuncao> results = (List<CadFuncao>) getSession()
					.createCriteria(
							"br.com.knowledge.persistence.hibernate.entities.CadFuncao")
					.add(create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

    @SuppressWarnings("unchecked")
    public List<CadFuncao> listAllBy(String property) {
            log.debug("list all CadFuncao instance ordered by property");
            try {
                    /*String sql = "select * from cad_funcao order by ?";
                    List<CadFuncao> results = (List<CadFuncao>) getSession()
                                    .createSQLQuery(sql).setString(0, property).list();*/
                    String hql = "from CadFuncao c order by nmfuncao";
                    List<CadFuncao> results = getSession().createQuery(hql).list();
                    return results;
            } catch (RuntimeException re) {
                    log.error("find by example failed", re);
                    throw re;
            }
    }

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding CadFuncao instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from CadFuncao as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<CadFuncao> findByNmfuncao(Object nmfuncao) {
		return findByProperty(NMFUNCAO, nmfuncao);
	}

	public List<CadFuncao> findByDescricao(Object descricao) {
		return findByProperty(DESCRICAO, descricao);
	}

	public List findAll() {
		log.debug("finding all CadFuncao instances");
		try {
			String queryString = "from CadFuncao";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public CadFuncao merge(CadFuncao detachedInstance) {
		log.debug("merging CadFuncao instance");
		try {
			CadFuncao result = (CadFuncao) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(CadFuncao instance) {
		log.debug("attaching dirty CadFuncao instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(CadFuncao instance) {
		log.debug("attaching clean CadFuncao instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}